// DB库

const MongoClient = require("mongodb").MongoClient;

const ObjectID = require('mongodb').ObjectId;

const Config = require('./config');

class Db {
    static getInstance() {
        // 单例模式防止重复连接数据库
        if (!Db.instance) {
            Db.instance = new Db();
        }
        return Db.instance;
    }
    constructor() {
        this.dbClient = ""; /**保存数据库 */
        this.connect(); // 实例化连接数据库
    }

    connect() {
        return new Promise((resolve, reject) => {
            if (!this.dbClient) { // 处理数据库多次连接
                MongoClient.connect(Config.dbUrl, (err, client) => {
                    console.log('连接中。。。')
                    if (err) {
                        reject(err);
                        return
                    }
                    this.dbClient = client.db(Config.dbName);
                    console.log('数据库连接成功')
                    resolve(this.dbClient);
                })
            } else {
                resolve(this.dbClient);
            }
        })

    }
    find(collectionName, json1, json2, json3) {
        let attr, slipNum, pageSize, page, sortJson;
        if (arguments.length === 2) {
            attr = {};
            slipNum = 0;
            pageSize = 0;
        } else if (arguments.length === 3) {
            attr = json2;
            slipNum = 0;
            pageSize = 0;
        } else if (arguments.length === 4) {
            attr = json2;
            page = json3.page || 1;
            pageSize = json3.pageSize || 20;
            slipNum = (page - 1) * pageSize;
            if (json3.sortJson) {
                sortJson = json3.sortJson;
            } else {
                sortJson = {}
            }
        } else {
            console.log('传入参数有误');
        }
        return new Promise((resolve, reject) => {
            this.connect().then(db => {
                // fields 查询的字段
                const result = db.collection(collectionName).find(json1, { fields: attr }).skip(slipNum).limit(pageSize).sort(sortJson);
                result.toArray((err, docs) => {
                    if (err) {
                        reject(err);
                        return
                    }
                    resolve(docs);
                })
            });
        })
    }
    update(collectionName, jsonNew, jsonOld) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).updateOne(jsonNew, {
                    $set: jsonOld
                }, (err, result) => {
                    if (err) {
                        reject(err)
                    }
                    resolve(result);
                })
            })
        })
    }
    insert(collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).insertOne(json, (err, result) => {
                    if (err) {
                        reject(err);
                    }
                    resolve(result);
                })
            })
        })
    }
    remove(collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                db.collection(collectionName).deleteOne(json, (err, result) => {
                    if (err) {
                        reject(err);
                    }
                    resolve(result);
                })
            })
        })
    }
    getObjectId(id) {
        return new ObjectID(id);
    }

    //统计数量的方法
    count(collectionName, json) {
        return new Promise((resolve, reject) => {
            this.connect().then((db) => {
                let result = db.collection(collectionName).count(json);
                result.then(function(count) {
                    resolve(count);
                })
            })
        })
    }
}
module.exports = Db.getInstance();